
package Sgdb::DataMessagesPane ;

use Sgdb::Utils qw (:DEFAULT
                    ) ;

use warnings ;
use strict ;
use integer ;
use bytes ;

my $text ;

init () ;

sub init {

    hook_add ( 'telthem',    => [\&send_data_pane_message , ''    ] ,
               'telthemloud' => [\&send_data_pane_message,  'loud'] ,
             ) ;
    1 ;
}

sub new {
    my $self = shift or die "whoami" ;
    my $topf = shift or die "top frame needed" ;

    $text = $topf -> Text ( -height      => 15,
                            pref 'message-win-text'
                            ) ;

    $text -> tagConfigure('time', -foreground => 'red');

#    balloonset $text =><<DoC ;
#This pane displays all messages accumulated
#in the course of your Sgdb session. You can
#use it not only as a monitor but also
#as a scratch area since it provides some
#basic editing capabilities. Try right-click!
#DoC

    my $m = $text -> menu() ;
    $m -> delete( 0 ) ;
    $m -> add ( 'separator' ) ;
    $m -> add ( 'command',
                -label => 'Close',
                -command  => [\&hook_run, 'close-current-data-pane' ],
                ) ;
    {
    my $t = $text ;
    $t -> bind ("<Meta-Up>",     sub {}) ;
    $t -> bind ("<Meta-Down>",   sub {}) ;
    $t -> bind (        "<4>", [$t => 'yviewScroll', -3, 'units' ]) ; # mouse wheel up
    $t -> bind (        "<5>", [$t => 'yviewScroll', +3, 'units' ]) ; # and down
    $t -> bind (  "<Shift-4>", [$t => 'yviewScroll', -1, 'units' ]) ; # mouse wheel up
    $t -> bind (  "<Shift-5>", [$t => 'yviewScroll', +1, 'units' ]) ; # and down
    $t -> bind ("<Control-4>", [$t => 'yviewScroll', -1, 'page'  ]) ;
    $t -> bind ("<Control-5>", [$t => 'yviewScroll', +1, 'page'  ]) ;
    }

    $text -> tagConfigure( 'messages',
                           -foreground => 'blue',
                           ) ;
    $text -> tagConfigure( 'yelling' ,
                           -foreground => 'red',
                           -background => 'lightblue',
                           ) ;

    pref_reconfigure 'message-win-text' => $text  ;

    hook_add( 'new-symbol-file', [\&lookup_syren_release_number]) ;

    $text ;
}

sub send_data_pane_message {

    my $t = $text ;
    return unless istkwidget $t ;

    my $tag  = shift ;

    if ($tag and $tag eq 'loud' ) { $tag = 'yelling'  }
    else                          { $tag = 'messages' }

    my ($s, $m, $h) = localtime( time() ) ;

    my $time = "($h:$m:$s)" ;
    my $msg = join (' ', @_ ) ;
    $msg =~ s/\s+\z//s ;

    $t -> insert('end',
                 $time . (' ' x (11 - length $time )) , 'time',
                 "$msg\n", $tag
                 ) ;
    $t -> see ('end' ) ;
}

sub lookup_syren_release_number {

    my $release ;

    $release = gdb_cached( 'syren-v' ) or return ; # gdb_exec_quiet ( "show emc release" ) ;
    $release =~ s/EMC release information: Major Minor release and tmp fixes is/Release: / ;
    chomp $release ;
    telthem $release ;
}
